<template>
  <div>
    <el-form :model="userPosts" label-width="100">
      <el-form-item label="标题">
        <el-input v-model="userPosts.title"></el-input>
      </el-form-item>
      <el-form-item label="描述/副标题">
        <el-input v-model="userPosts.description"></el-input>
      </el-form-item>
      <el-form-item label="标签">
        <el-input v-model="userPosts.label"></el-input>
      </el-form-item>
      <el-form-item label="何人可见">
        <el-select
            v-model="userPosts.status"
            placeholder="请选择"
            style="width: 240px"
        >
          <el-option :key="1" label="所有人" :value="0" />
          <el-option :key="1" label="个人" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="内容">
        <el-upload
            :action="$upFile"
            name="files"
            multiple
            :data="fileInfoVo"
            :on-success="getUploadIdToContent"
        >
          <el-button type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="封面">
        <el-upload
            :action="$upFile"
            class="avatar-uploader"
            name="files"
            multiple
            :data="fileInfoVo"
            :limit="1"
            :on-success="getUploadIdToCover">
          <img v-if="userPosts.coverId" :src="$img+userPosts.coverId" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="savePosts">
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>

import {reactive} from "vue";
import {createUserPosts} from "@/api/user";

let userPosts = reactive({
  title: '',//标题
  description: '',//描述、副标题
  status: 0,
  label: '',
  coverId: '',//封面
  fileIds: []
})
const fileInfoVo = {
  bucketName: 'user',
  folderName: 'posts',
}

function getUploadIdToContent(res) {
  userPosts.fileIds.push(res.data)
}

function getUploadIdToCover(res) {
  userPosts.coverId = res.data
}
function savePosts() {
  createUserPosts(userPosts)
}
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
